package com.mini.task;

import com.alibaba.fastjson.JSONObject;
import com.mini.constant.RedisKeyConst;
import com.mini.model.BusinessBaseInfo;
import com.mini.model.BusinessPlanSiteHistory;
import com.mini.model.BusinessPlanSitePageR;
import com.mini.service.BaseService;
import com.mini.service.BusinessBaseService;
import com.mini.service.BusinessPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @author gaoyanfeng
 * @title: 次日小课站点凌晨更新
 */
@Component
@EnableScheduling
@Configuration
@Slf4j
public class NextDayLessonSiteTask extends BaseService {

    @Autowired
    private BusinessPlanService planService;
    @Autowired
    private BusinessBaseService baseService;

    @Autowired
    protected RedisTemplate redisTemplate;

    @Scheduled(cron = "${plan.nextdaylessonsite.time}")
    public void nextDayLessonSiteUpdate()  {

        String opreationLock = RedisKeyConst.getOpreationLock("plan_nextdaylessonsite");
        if(tryLock(opreationLock)) {
            try {
                log.info("次日小课站点变更");
                Set<String> members = redisTemplate.opsForSet().members(RedisKeyConst.PLAN_NEXTDAYLESSONSITE);
                log.info("次日小课站点变更members"+ JSONObject.toJSONString(members));
                if (null == members) {
                    return;
                }
                for (String planNextDayLessonSite : members) {
                    String[] split = planNextDayLessonSite.split("_");
                    Long planSite = Long.valueOf(split[0]);
                    Long nextDayLessonSite = Long.valueOf(split[1]);

                    BusinessPlanSitePageR planSitePageR = planService.getRByPlanSite(planSite);
                    BusinessPlanSiteHistory businessPlanSiteHistory = new BusinessPlanSiteHistory(planSite,planSitePageR.getPlanName(),planSitePageR.getLessonSite(), planSitePageR.getNextDayLessonSite());
                    // 保存历史站点
                    planService.saveHistory(businessPlanSiteHistory);
                    // 更新关系表
                    planSitePageR.setNextDayLessonSite(0L);
                    planSitePageR.setLessonSite(nextDayLessonSite);
                    BusinessBaseInfo baseInfo = baseService.getByMiniLessonId(nextDayLessonSite);
                    planSitePageR.setGroupIds(baseInfo.getQuantumGroupId());
                    planSitePageR.setGroupNames(baseInfo.getQuantumGroupName());
                    planSitePageR.setChannelCode(baseInfo.getChannelCode());
                    planSitePageR.setChannelId(baseInfo.getChannelId());
                    planSitePageR.setChannelName(baseInfo.getChannelName());
                    planSitePageR.setWorkRoomId(baseInfo.getArmyGroupId()==null?0L:Long.parseLong(baseInfo.getArmyGroupId()));
                    planSitePageR.setWorkRoomName(baseInfo.getArmyGroupName());
                    planService.saveR(planSitePageR);
                    redisTemplate.opsForSet().remove(RedisKeyConst.PLAN_NEXTDAYLESSONSITE, planNextDayLessonSite);
                }
            } catch (Exception ex) {
                log.error("task-plan_nextdaylessonsite error ,ex = {} ",ex);
                releaseLockKey(opreationLock);
            }finally {
                releaseLockKey(opreationLock);
            }
        }
    }

}
